How to Enable Java Preview Features

Historically Java had a very slow release schedule with only nine major versions released in more than 20 years. This changed in 2018, when Oracle decided to start to release a new major version every six month. Since then as of early 2023, we got 9 new Java releases in less than five years.

With this very frequent release schedule Oracle decided to release new Java language features as a preview bundled in the regular major release. This means that you only need the latest regular JDK installed to try the new features that are previewed in that version.

By this the Java community can give early feedback and look for defects before the feature goes into the regular release.

Enable Previews in the Command Line

However, these features are by default disabled and you will need to provide the compiler flag –enable-preview to the javac compiler to enable the preview feature for compilation. Additionally you need to provide the –release $no flag to specify the version of the JDK. The version must always be the version of the JDK you have installed. You cannot enable preview features of previous JDK, since these might already be generally released in your current version. If a feature preview is not released in a JDK version, it will get a second preview in the next one. By this, you always have a defined set of preview features for every major JDK version.

Here is a quick example how to enable previews for JDK 19:

javac Bar.java --release 19 --enable-preview

But you cannot enable features from older JDKs:

javac Bar.java -- release 18 --enable-preview // This will not work with JDK 19

A similar mechanics applies when you want to run your program from the command line. The java command also takes a –enable-preview flag to start with preview features enabled. This time there is no –release flag required:

java --enable-preview Bar

This also works for applications in a jar file:

java --enable-preview -jar App.jar

Maven Plugin Configurations to Enable Preview Features

If you use Maven to compile and test your application, you can simple add the required flags to the maven-compiler-plugin in the plugins section of your pom:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <release>19</release>
    <compilerArgs>
      <arg>--enable-preview</arg>
    </compilerArgs>
  </configuration>
</plugin>

In line (6) the release version is specified and in line (7) to (8) the –enable-preview flag.

If you want to use preview features in your unit test too, you need to enable previews in the maven-surefire-plugin that will run your Junit tests:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.2</version>
  <configuration>
    <includes>
      <include>**/*Test.java </include>
    </includes>
    <reuseForks>true</reuseForks>
    <argLine>--enable-preview</argLine>
  </configuration>
</plugin>

If you use the above Maven plugin configurations, most IDEs will also start the application automatically with previews enabled.

For example in Eclipse, you will get a project configuration with the preview launch settings enabled which works for both, compilation and running the code:

preview flag in Eclipse project settings

VS Code will also use the Maven compiler plugin configuration when compiling your code and will also add the the preview flag automatically to the java command when a terminal is opened to execute your code.

preview flag in VS Code

However, if you want to use incubator features in VS Code, you will need to add the incubator modules manually to the java command in the terminal. As the time of writing there is no option to do this automatically.

Finally, in IntelliJ you open the project settings (Ctrl+Alt+Shift+S) and select the Java version you would like to test and select „SDK – X Experimental Features“ from the Language level dropdown:

Make sure that under the Modules tab this option is selected as well. I had cases when the second one didn’t change automatically with the first option:

This settings will enable preview features in IntelliJ. If you want to use incubator features too, you will nee to create your own runtime configuration and add the required incubator module by an additional VM option. Here is an example to enable ScopedValues for Java 20 in IntelliJ: